// pages/member/member.js
const appInstance = getApp()
var url = appInstance.globalData.url
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    imgUrls: [],
    classifyList: [],
    indicatorDots: true,
    autoplay: true,
    circular: true,
    interval: 5000,
    duration: 1000,
    currentSwiper: 0,
    videoList: []
  },

  openMember() {
    wx.navigateTo({
      url: './open_member/open_member',
    })
  },
  //点击进入H5页面
  swipclick(e) {
    var Id = e.currentTarget.id;
    wx.navigateTo({
      url: '../test/test?id=' + Id
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(wx.getStorageSync('is_member'))
    this.getLubo();
    this.OnClassifyList();
    this.setData({
      is_member: wx.getStorageSync('is_member')
      // is_member:1
    })
//判断如果是会员
    if(this.data.is_member == 2){
      this.setData({
        num:'0',
        orientation:'20px'
      })
    }
  },
  //banner图
  getLubo() {
    var that = this;
    wx.request({
      url: url + '/banner/list',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: 'POST',
      // data: {
      //   is_member: appInstance.globalData.is_member
      // },
      dataType: 'json',
      success(res) {
        console.log(res.data.data);
        that.setData({
          imgUrls: res.data.data
        })
      }
    })
  },

  //分类列表
  OnClassifyList() {
    var that = this;
    wx.request({
      url: url + '/category/list',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        res.data.data.forEach(item => {
          item.videoList = []
        })
        that.setData({
          classifyList: res.data.data
        }, function() {
          that.onChange({
            detail: {
              index: that.data.active
            }
          })
        })
      }
    })
  },

  //分类视频
  onChange(e) {
    var index = e.detail.index;
    if (this.data.classifyList[index].videoList.length) {
      return false;
    }
    //获取分类id
    var id = this.data.classifyList[index].id
    var that = this;
    wx.request({
      url: url + '/video/list',
      method: 'POST',
      data: {
        is_charge: wx.getStorageSync('is_member'),
        category_id: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        console.log(res)
        const tempData = that.data.classifyList;
        const item = tempData[index];
        const list = item.videoList;
        if (list) {
          item.currPage++;
          item.videoList = list.concat(res.data.data);
        } else {
          item.currPage = 1;
          item.videoList = res.data.data;
        }
        that.setData({
          classifyList: tempData
        })
      }
    })
  },

  bindChange(e) {
    this.setData({
      currentSwiper: e.detail.current
    })
  },

  //进入详情
  videtDetail(e) {
    // console.log(e.currentTarget.id)
    var Id = e.currentTarget.id;
    wx.navigateTo({
      url: '../index/classify/videoDetail/videoDetail?id=' + Id,
    })
    // wx.request({
    //   url: url + '/video/detail',
    //   method: 'POST',
    //   data: {
    //     video_id: e.currentTarget.id,
    //     pp_token: wx.getStorageSync('to_ken')
    //   },
    //   header: {
    //     'content-type': 'application/x-www-form-urlencoded' // 默认值
    //   },
    //   dataType: 'json',
    //   success(res) {
    //     console.log(res)
    //     const tempData = that.data.classifyList;
    //     const item = tempData[index];
    //     const list = item.videoList;
    //     if (list) {
    //       item.currPage++;
    //       item.videoList = list.concat(res.data.data);
    //     } else {
    //       item.currPage = 1;
    //       item.videoList = res.data.data;
    //     }
    //     that.setData({
    //       classifyList: tempData
    //     })
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})